package cn.sccl.dirms.topo.discovery.task;

import cn.sccl.common.scheduler.enums.JobStateEnum;
import cn.sccl.common.scheduler.model.TaskFireLog;
import cn.sccl.common.scheduler.model.TaskParams;
import cn.sccl.common.scheduler.service.TaskFireLogManager;
import cn.sccl.dirms.topo.discovery.model.DiscoverTask;
import cn.sccl.dirms.topo.discovery.service.DiscoverTaskManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 自动发现任务服务
 *
 * @author _minus
 * @create 2017-10-12 23:57
 */
@Service("discoverJobService")
public class DiscoverJobService {


    @Autowired
    private DiscoveryService discoveryService;


    @Autowired
    private DiscoverTaskManager discoverTaskManager;

    @Autowired
    private TaskFireLogManager taskFireLogManager ;

    /**
     *
     * @param taskParams  {params：taskId ,fireLogId:fireLogId}
     */
    public void  startDiscoverTask(TaskParams taskParams) throws InterruptedException {

        if (taskParams.validateData()){
            String taskJson = taskParams.getParams().toString();
            DiscoverTask task = com.alibaba.fastjson.JSONObject.parseObject(taskJson,DiscoverTask.class);


            TaskFireLog log = taskFireLogManager.get(taskParams.getFireLogId()) ;
            //将log的状态改为正在执行中

            log.setStatus(JobStateEnum.TASK_RUNNING.getCode().toString());//任务状态为正在执行
            taskFireLogManager.save(log);

            discoveryService.callTask(task,taskParams.getFireLogId());
        }


    }

}
